﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using NabNetLib.Apps.Interface;
using agsXMPP;
using agsXMPP.protocol.client;

namespace NabNetLib.Apps.Mood
{
    public class MoodPlugin : IPlugin
    {

        private List<string> mMoodList;
        private object mLocker = new object();
        private static Random mRandomizer = new Random((int)(DateTime.Now.Ticks % Int32.MaxValue));
        private static Dictionary<string,List<string>> mMoodHistory = new Dictionary<string, List<string>>();

        #region IPlugin Members

        public bool HandleMessage(INabNetMessage msg, ref Packet.Packet answer)
        {
            switch(msg.MessageType)
            {
                case NabNetMessageType.Button:
                case NabNetMessageType.Rfid:
                case NabNetMessageType.Scheduler:
                    {
                        #region get random mood in order to have a different enough mood
                        if(!mMoodHistory.ContainsKey(msg.To))
                        {
                            mMoodHistory.Add(msg.To,new List<string>());
                        }
                        List<string> moodHistory = mMoodHistory[msg.To];
                        
                        string mood = GetRandomMood();
                        while(moodHistory.Contains(mood))
                        {
                            mood = GetRandomMood();
                        }

                        moodHistory.Add(mood);
                        
                        while(moodHistory.Count>10)
                        {
                            moodHistory.RemoveAt(0);
                        }
                        #endregion


                        string message = "MU broadcast/vl/" + mood + "\nPL 3\nMW\n";
                        Packet.MessagePacket packet =
                            new Packet.MessagePacket(message);

                        answer = packet;
                        
                    }
                    return true;
                default :
                    return false;
            }
        }

        #endregion

        private string GetRandomMood()
        {
            lock (mLocker)
            {
                if (mMoodList == null || mMoodList.Count == 0)
                {
                    mMoodList = new List<string>();
                    string moodDirectory = Path.Combine(
                        ConfigurationManager.AppSettings[Constants.BROADCAST_BASE_DIRECTORY_KEY],
                        ConfigurationManager.AppSettings[Constants.MOOD_RELATIVE_PATH_KEY]);
                    foreach (string f in Directory.GetFiles(moodDirectory, "*.mp3"))
                    {
                        string relativePath =
                            ConfigurationManager.AppSettings[Constants.MOOD_RELATIVE_PATH_KEY].Replace(@"\", "/");
                        mMoodList.Add(relativePath + Path.GetFileName(f));
                    }
                }
            }

            int moodIndex = mRandomizer.Next(0, mMoodList.Count - 1);

            return mMoodList[moodIndex];
        }
    }
}
